import urllib.request
import re
import requests
from bs4 import BeautifulSoup
import os
import time

imagePath="python/python.robot/wechat_image/"
#https://fabiaoqing.com/biaoqing/lists/page/2.html
def getHtmlStr(_url):
    req=urllib.request.urlopen(_url)
    res=req.read().decode('utf-8')
    return res

# ui image lazy
def savePic(_html):
    soup=BeautifulSoup(_html,'html.parser')
    imgs=soup.find_all('img',attrs={'class':'ui image lazy'})
    if not os.path.exists(imagePath):
        os.makedirs(imagePath)
    x=(startPage-1)*pageSize
    for item in imgs:
        src=item.get('data-original')
        if not src==None:
            urllib.request.urlretrieve(src,str(imagePath+'{}.png').format(x))
            x+=1

#定义从第一页开始爬多少页
startPage=1
pageNumber=10
pageSize=44
pageUrl="https://fabiaoqing.com/biaoqing/lists/page/{}.html"
while startPage<pageNumber:
    html= getHtmlStr(pageUrl.format(startPage))
    savePic(html)
    startPage+=1


